package com.iemes.controller.production;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.OrderFormMap;
import com.iemes.entity.WorKOrderFormMap;
import com.iemes.mapper.production.WorkOrderMapper;
import com.iemes.util.Common;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/workOrder/")
public class WorkOrderController extends BaseController {

    @Inject
    private WorkOrderMapper workOrderMapper;
    @RequestMapping("workOrderIndex")
    public String index(Model model){
        String[] btnArray = new String[]{"<button type=\"button\" id=\"addAccount\" class=\"btn btn-primary marR10\">新增</button>", "<button type=\"button\" id=\"editAccount\" class=\"btn btn-info marR10\">编辑</button>", "<button type=\"button\" id=\"delAccount\" class=\"btn btn-danger marR10\">删除</button>"};
        model.addAttribute("res", findByRes());
        model.addAttribute("btn", btnArray);
        return Common.BACKGROUND_PATH + "/production/WorkOrder/WorkOlderIndex";
    }

    @ResponseBody
    @RequestMapping("getWorkOrderPage")
    public PageInfo getWorkOrderPage(String pageNow,
                                     String pageSize, HttpServletRequest request) throws Exception {
//        PageHelper.startPage(1, 10);
        //采用pageinfo分页
        if(pageNow == null){
            PageHelper.startPage(1, 10);
        }else {
            PageHelper.startPage(Integer.parseInt(pageNow), 10);
        }
        List<WorKOrderFormMap> worKOrderFormMaps = workOrderMapper.selWorkOrderPage();
        for (WorKOrderFormMap work : worKOrderFormMaps){
            if(work.get("matterpick").toString().equals("true")){
                work.set("matterpick","已完成");
            }else {
                work.set("matterpick","未完成");
            }
        }
        PageInfo pageInfo = new PageInfo<>(worKOrderFormMaps);
        return pageInfo;
    }

    //添加数据
    @ResponseBody
    @RequestMapping("addWorkOrder")
    //    @SystemLog(module="系统管理",methods="客户管理-新增客户")//凡需要处理业务逻辑的.都需要记录操作日志
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
    public String addWorkOrder(){
        WorKOrderFormMap worKOrderFormMap = getFormMap(WorKOrderFormMap.class);
        try {
            workOrderMapper.addEntity(worKOrderFormMap);//添加数据
            //修改数据，将对应的订单号修改成不能使用
//            workOrderMapper.upOrder(worKOrderFormMap.get("ipedemid").toString());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }

    @RequestMapping("openWorkOrderManagementUI")
    public String openWorkOrderManagementUI(Model model){
        model.addAttribute("client",getClient());
        model.addAttribute("emp",getEmp());
        model.addAttribute("dep",getDep());
        //获取订单
        model.addAttribute("order",workOrderMapper.getOrder());
        return Common.BACKGROUND_PATH + "/production/WorkOrder/addWordOrder";
    }

    @RequestMapping("editWorkOrderManagementUI")
    public String editWorkOrderManagementUI(Model model, HttpServletRequest request){
        String id = getPara("id");
        if(Common.isNotEmpty(id)){
            model.addAttribute("client",getClient());
            model.addAttribute("dep",getDep());
            model.addAttribute("emp",getEmp());
            model.addAttribute("user", workOrderMapper.findbyFrist("bpid", id, WorKOrderFormMap.class));
        }
        return Common.BACKGROUND_PATH + "/production/WorkOrder/editWordOrder";
    }


    @ResponseBody
    @RequestMapping("editWorkOrder")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
//    @SystemLog(module="系统管理",methods="用户管理-修改用户")//凡需要处理业务逻辑的.都需要记录操作日志
    public String editOrder(String txtGroupsSelect) throws Exception {
        WorKOrderFormMap worKOrderFormMap = getFormMap(WorKOrderFormMap.class);
//        worKOrderFormMap.put("txtGroupsSelect", txtGroupsSelect);
//        workOrderMapper.deleteByAttribute("bpid", worKOrderFormMap.get("bpid")+"", WorKOrderFormMap.class);
////        String lyBom = orderMapper.findLyBom(orderFormMap.get("stockid").toString());
////        orderFormMap.put("stockid", lyBom);
//        workOrderMapper.addEntity(worKOrderFormMap);
        workOrderMapper.editEntity(worKOrderFormMap);
        return "success";
    }

    @ResponseBody
    @RequestMapping("deleteWorkOrder")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
    @SystemLog(module="系统管理",methods="用户管理-删除用户")//凡需要处理业务逻辑的.都需要记录操作日志
    public String delOrder() throws Exception {
        String[] ids = getParaValues("ids");
        for (String id : ids) {
            workOrderMapper.deleteByAttribute("bpid", id, WorKOrderFormMap.class);
        }
        return "success";
    }


    @ResponseBody
    @RequestMapping("createOrder")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
    public String createOrder(Model model, HttpServletRequest request){
        //需要查询此计划单号是否已生成订单

        String id = request.getParameter("id");//bpid
        String orderNum = request.getParameter("orderNum");//前端生成的订单号
        System.out.println("id+orderNum" + id + "," + orderNum);
        WorKOrderFormMap worKOrderFormMap = workOrderMapper.findbyFrist("bpid", id, WorKOrderFormMap.class);


        //创建生成订单需要数据类型
        OrderFormMap orderFormMap = new OrderFormMap();
        orderFormMap.set("ofcode",orderNum);//订单号
        orderFormMap.set("edemid",worKOrderFormMap.get("bpedemid"));
        orderFormMap.set("stockid",worKOrderFormMap.get("stockid"));
        orderFormMap.set("stockname",worKOrderFormMap.get("stockname"));
        orderFormMap.set("num",worKOrderFormMap.get("num"));
        orderFormMap.set("billstateid",3);//下达
        orderFormMap.set("workshopId",worKOrderFormMap.get("depid"));
        orderFormMap.set("btime",worKOrderFormMap.get("bpbtime"));
        orderFormMap.set("etime",worKOrderFormMap.get("bpetime"));
        orderFormMap.set("bpid",id);//将生产计划id存入订单表，以建立关联

        //生成订单
        try {
            workOrderMapper.addEntity(orderFormMap);//添加数据

            worKOrderFormMap.set("ofid",orderNum);//修改计划表信息，将订单编号添加到计划表中，以建立关联
            worKOrderFormMap.remove("matterpick");//重新修改数据时需要设置matterpick，否则会报错Data too long for column 'matterpick'
            workOrderMapper.editEntity(worKOrderFormMap);
        } catch (Exception e) {
            e.printStackTrace();
        }


        return "success";
    }
}


